// 首页相关操作
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { ANY_OBJECT } from '@/types/generic';
import { FlowOperationController } from '@/api/flow';
import { SysFlowTaskOperationType } from '@/common/staticDict/flow';
import LoginController from '@/api/system/LoginController';
// 待办消息
export const welcomeDb = () => {
  const router = useRouter();
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(6);
  const total = ref<number>(0);
  const dataList = ref<ANY_OBJECT[]>([]);
  const loadData = () => {
    const params = {
      orderParam: [
        {
          fieldName: 'entryId',
          asc: true,
        },
      ],
      pageParam: {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      },
    };
    FlowOperationController.listRuntimeTask(params)
      .then(res => {
        dataList.value = res.data.dataList;
        total.value = res.data.totalCount;
      })
      .catch(e => {
        ElMessage.error(e);
      });
  };
  const onSubmit = (row: ANY_OBJECT) => {
    const params = {
      processInstanceId: row.processInstanceId,
      processDefinitionId: row.processDefinitionId,
      taskId: row.taskId,
    };
    FlowOperationController.viewRuntimeTaskInfo(params)
      .then(res => {
        console.log('......', res);
        if (res.data) {
          router.push({
            name: res.data.routerName || 'handlerFlowTask',
            query: {
              isRuntime: 'true',
              isDraft: row.isDraft,
              taskId: row.taskId,
              processDefinitionKey: row.processDefinitionKey,
              processInstanceId: row.processInstanceId,
              processDefinitionId: row.processDefinitionId,
              formId: res.data.formId,
              routerName: res.data.routerName,
              readOnly: res.data.readOnly,
              taskName: row.taskName,
              flowEntryName: row.processDefinitionName,
              processInstanceInitiator: row.showName,
              // 过滤掉加签、减签操作，加签、减签只有在已完成任务里可以操作
              operationList: JSON.stringify(
                (res.data.operationList || []).filter((item: ANY_OBJECT) => {
                  return (
                    item.type !== SysFlowTaskOperationType.CO_SIGN &&
                    item.type !== SysFlowTaskOperationType.REVOKE &&
                    item.type !== SysFlowTaskOperationType.SIGN_REDUCTION
                  );
                }),
              ),
              variableList: JSON.stringify(res.data.variableList),
            },
          });
        }
      })
      .catch(e => {
        console.warn(e);
      });
  };
  return {
    pageNum,
    pageSize,
    total,
    dataList,
    loadData,
    onSubmit,
  };
};

// 催办
export const welcomeCb = () => {
  const router = useRouter();
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(4);
  const totalCb = ref<number>(0);
  const cbList = ref<ANY_OBJECT[]>([]);
  const loadCb = () => {
    const params = {
      orderParam: [
        {
          fieldName: 'createTime',
          asc: true,
        },
      ],
      pageParam: {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      },
    };
    FlowOperationController.listRemindingTask(params)
      .then(res => {
        cbList.value = res.data.dataList;
        totalCb.value = res.data.totalCount;
      })
      .catch(e => {
        ElMessage.error(e);
      });
  };
  const onSubmitCb = (row: ANY_OBJECT, messageId: string | null = null) => {
    // 是否抄送消息
    const isCopy = messageId != null;
    const params = {
      processInstanceId: row.processInstanceId,
      processDefinitionId: row.processDefinitionId,
      taskId: row.taskId,
    };
    const httpCall = isCopy
      ? FlowOperationController.viewInitialHistoricTaskInfo(params)
      : FlowOperationController.viewRuntimeTaskInfo(params);
    httpCall
      .then(res => {
        if (res.data) {
          router.push({
            name: res.data.routerName || 'handlerFlowTask',
            query: {
              isRuntime: !isCopy + '',
              taskId: row.taskId,
              messageId: messageId,
              processDefinitionKey: row.processDefinitionKey,
              processInstanceId: row.processInstanceId,
              processDefinitionId: row.processDefinitionId,
              formId: res.data.formId,
              routerName: res.data.routerName,
              readOnly: (isCopy ? true : res.data.readOnly) + '',
              taskName: row.taskName,
              flowEntryName: row.processDefinitionName,
              processInstanceInitiator: row.processInstanceInitiator,
              // 过滤掉加签和撤销操作，加签只有在已完成任务里可以操作
              operationList: JSON.stringify(
                (res.data.operationList || []).filter((item: ANY_OBJECT) => {
                  return (
                    item.type !== SysFlowTaskOperationType.CO_SIGN &&
                    item.type !== SysFlowTaskOperationType.REVOKE &&
                    item.type !== SysFlowTaskOperationType.SIGN_REDUCTION
                  );
                }),
              ),
              variableList: JSON.stringify(res.data.variableList),
            },
          });
        }
      })
      .catch(e => {
        console.warn(e);
      });
  };
  return {
    pageNum,
    pageSize,
    totalCb,
    cbList,
    loadCb,
    onSubmitCb,
  };
};

// 抄送消息
export const welcomeCopy = () => {
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(8);
  const totalCopy = ref<number>(0);
  const copyList = ref<ANY_OBJECT[]>([]);
  const loadCopy = () => {
    const params = {
      orderParam: [
        {
          fieldName: 'createTime',
          asc: true,
        },
      ],
      pageParam: {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
      },
    };
    FlowOperationController.listCopyMessage(params)
      .then(res => {
        copyList.value = res.data.dataList;
        totalCopy.value = res.data.totalCount;
      })
      .catch(e => {
        ElMessage.error(e);
      });
  };

  return {
    pageNum,
    pageSize,
    totalCopy,
    copyList,
    loadCopy,
  };
};

// 查询用户工号
export const getWorkNo = () => {
  const userName = ref<string>('');
  LoginController.getUserInfo()
    .then((res: any) => {
      userName.value = res.data;
    })
    .catch(e => {
      ElMessage.error(e);
    });
  return {
    userName,
  };
};
